--[[
导出光卡
]]
local LevelDump = {}



function LevelDump.dump(mode)
	local levelData =  mode.levelData_; 
	
	local lines = {}

    lines[#lines + 1] = ""
    lines[#lines + 1] = string.format("------------ MAP %s ------------", levelData.levelId)
    lines[#lines + 1] = ""
    lines[#lines + 1] = "local map = {}"
    lines[#lines + 1] = ""
     lines[#lines + 1] = ""
     
     
     
    lines[#lines + 1] = string.format("map.levelId = %d", levelData.levelId)
    lines[#lines + 1] = string.format("map.levelType = %d --0表示步数模式 1表示能量模式", levelData.levelType)
    lines[#lines + 1] = string.format("map.energyFullNums = %d --总能量值（次数）  表示多少个每一圈的能量值", levelData.energyFullNums)
    lines[#lines + 1] = string.format("map.energyNums = %d --每一圈的能量值    秒", levelData.energyNums)    
    lines[#lines + 1] = string.format("map.levelConditionType = %d --0规定时间内达到某个分数，1收集特定宝石，2将种道具移动到下层", levelData.levelConditionType)
 	lines[#lines + 1] = string.format("map.levelConditionParam1 = %s", levelData.levelConditionParam1)
 	lines[#lines + 1] = string.format("map.levelConditionParam2 = %s", levelData.levelConditionParam2)
    lines[#lines + 1] = string.format("map.sceneSound = \"%s\"", levelData.sceneSound)
    lines[#lines + 1] = ""
	lines[#lines + 1] = ""
	lines[#lines + 1] = ""
	lines[#lines + 1] = ""
	
    -- objects
    --local allid = table.keys(self.objects_)
   	--table.sort(allid)
   	lines[#lines + 1] = "--地图的静态物体数据"
    lines[#lines + 1] = "local static = {}"
    --for i, id in ipairs(allid) do
        lines[#lines + 1] = ""
        local role = mode:getObject("static:0_0")
    	local cardSpriteId= role:getCardSpriteId();
    		
        lines[#lines + 1] = "local object = {"
        lines[#lines + 1] =  "	" .. "cardSpriteId = "..cardSpriteId..","
        lines[#lines + 1] =  "	" .. "focusObject = true,"
        lines[#lines + 1] =  "	" .. "defineId = 'cardSprite'"
        lines[#lines + 1] = "}"
    --end
    lines[#lines + 1] = ""
    lines[#lines + 1] = "static['static:0_0'] = object"
    lines[#lines + 1] = ""
    lines[#lines + 1] = "map.static = static"
    lines[#lines + 1] = ""
    lines[#lines + 1] = ""
    lines[#lines + 1] = ""
    lines[#lines + 1] = ""
    
    
    
    --导出底图
    local col = mode.col_;
    local row = mode.row_;
    lines[#lines + 1] = "--地图的卡牌配置数据"
    lines[#lines + 1] = "map.cardSpriteData = {"
    for i = 1,col,1 do  --y遍历
    	local str = {};
    	for j = 1,row,1 do  --x遍历
    		local staticId = "static:" ..j .."_"..i ;
    		local obj = mode:getObject(staticId)
    		local cardSpriteId= obj:getCardSpriteId();
    		
    		--截取字符串
    		str[#str +1] = cardSpriteId;
    	end
    	
    	if i == col then
    		lines[#lines + 1] = "	{" .. table.concat(str, ", ").."}"
    	else
    		lines[#lines + 1] = "	{" .. table.concat(str, ", ") .. "},"
    	end
    end
    lines[#lines + 1] = "}"
    
    
    
    lines[#lines + 1] = ""
    lines[#lines + 1] = "return map"
    lines[#lines + 1] = ""
    return table.concat(lines, "\n")
end












--[[--

从 package.path 中查找指定模块的文件名，如果失败返回 false。

@param string moduleName
@return string

]]
function LevelDump.findModulePath(moduleName)
    local filename = string.gsub(moduleName, "%.", "/") .. ".lua"
    local paths = string.split(package.path, ";")
    for i, path in ipairs(paths) do
        if string.sub(path, -5) == "?.lua" then
            path = string.sub(path, 1, -6)
            if not string.find(path, "?", 1, true) then
                local fullpath = path .. filename
                if io.exists(fullpath) then
                    return fullpath
                end
            end
        end
    end
    
    return false
end





--[[--

导出地图数据到文件

]]
function LevelDump.dumpToFile(model)
    local contents = LevelDump.dump(model)
    local path = LevelDump.findModulePath(model.levelModuleName_)
    
    if path then
        printf("save data filename \"%s\" [%s]", path, os.date("%Y-%m-%d %H:%M:%S"))
        io.writefile(path, contents)
        return true
    else
    	local newPath=device.writablePath.."/src/";
    	local levelModuleName = string.split(model.levelModuleName_, ".")
    	local str = table.concat(levelModuleName, "\\\\")
    	newPath = newPath..str..".lua";
    	local file = io.open(newPath,"w")
    	file:write(contents);
    	file:write("\r\n");
    	file:close();
      
      
        printf("not found module file, dump [%s]", os.date("%Y-%m-%d %H:%M:%S"))
        --echoj("\n\n" .. contents .. "\n")
        return false
    end
end











--[[
function checkDirOK( path )
    require "lfs"
    local oldpath = lfs.currentdir()


    if lfs.chdir(path) then
        lfs.chdir(oldpath)
        print("路径检查OK->"..path)
        return true
    end

    if lfs.mkdir(path) then
        print("路径创建OK->"..path)
        return true
    end
end


function LevelDump.writefileCheckDir(path, data)
    local pathinfo = io.pathinfo(path)
    if checkDirOK(pathinfo.dirname) then
        io.writefile(path, data)
        return true
    else
        

        if device.platform == "windows" then
            local newStr = string.gsub(pathinfo.dirname, "/", "\\")
            print("开始创建目录："..newStr)
            os.execute("mkdir "..newStr)
        else
            os.execute("mkdir -p "..pathinfo.dirname)
        end

        io.writefile(path, data)
        return true
    end

    print("写入完成:"..path)
end
]]

return LevelDump
